<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\BaseController;
use App\Models\User\StaffModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;

class StaffController extends BaseController
{
    const STATUS_FORBIDDEN = 0;
    const STATUS_OK = 1;

    const STATUS = [
        self::STATUS_FORBIDDEN => '已禁用',
        self::STATUS_OK => '正常'
    ];

    const COLOR = [
        self::STATUS_FORBIDDEN => 'red',
        self::STATUS_OK => 'green'
    ];

    public function index(Content $content)
    {
        $grid = Grid::make(new StaffModel(), function (Grid $grid) {
            $grid->model()->where('status', '!=', -1)->orderBy('id', 'desc');
            $grid->column('name', '账号');
            $grid->column('realname', '名称')->label();
            //$grid->column('status', '状态')->using(self::STATUS)->label(self::COLOR);
            $grid->column('status', '状态')->switch();

            $grid->disableToolbar();
            $grid->disableActions();
            $grid->disableRowSelector();
            //$grid->simplePaginate();
            /*$grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
            });*/
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('realname', '名称');
                $filter->equal('status', '账号状态')->select(self::STATUS);
            });
        });

        return $content
            ->full()
            ->title('OA账号列表')
            ->body($grid);
    }

    protected function form()
    {
        return Form::make(new StaffModel(), function (Form $form) {
            $form->hidden('status')->customFormat(function ($v) {
                return $v == 1 ? '正常' : '已禁用';
            });
        });
    }
}
